package paylayer

import (
	"strconv"
	//"dhfshop/models/seller"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)
	
//订单支付消息通知
func OrderPayNotify(payNotify *CommonPayNotify)(err error) {
	var (
		orders     *shopping.ValueOrder
	)
	orderId, _:= strconv.ParseInt(payNotify.OrderId, 10, 64)
	if _, err = shopping.GetPaymentListByOrderId(orderId); err == orm.ErrNoRows {
	    orders, err = shopping.GetOrderById(orderId)
		if err != nil {
		    beego.Emergency(err.Error())
		    return
		}
		id, e := shopping.AddPaymentByPayMsg(orders.PartnerId,orders.StoreId,orders.MemberId,orderId,1,
                        "1",payNotify.TransactionId,payNotify.FeeType,
						payNotify.AppId,payNotify.MchId,"",payNotify.SrcMessage,
						payNotify.TotalFee,payNotify.CashFee,payNotify.CouponFee,payNotify.MchTypeId)			
		if e != nil {
		    err = e
			return
		}
		m := &shopping.ValueOrder{OrderId:orderId,CouponFee:payNotify.CouponFee}
		shopping.UpdateOrderCouponFee(m)
	    err = OrderPayAdditional(orderId)
		if err == nil {
		    if _, err = shopping.GetPintuanActivityId(orderId); err == nil {
			 
			    return
			}
			if _, err = shopping.GetPintuanId(orderId); err == nil {
			 
			    return
			}
		}
		//
		n := &shopping.PaymentList{PaymentListId:id,ProfitStatus:orders.ProfitStatus}
		shopping.UpdatePaymentProfit(n)
		//
		SendPrintMsg(orders.PartnerId,orders.StoreId,orders.OrderId)		
	}
	return
}

//余额支付结果
func BalancePayMsg(orderIdStr string)(err error) {
    var (
		orderId    int64
		orders     *shopping.ValueOrder
	)
	orderId, err = strconv.ParseInt(orderIdStr, 10, 64)
	if err != nil {
		beego.Emergency(err.Error())
		return
	}
	orders, err = shopping.GetOrderById(orderId)
	if err != nil {
		beego.Emergency(err.Error())
		return
	}
	if _, err = shopping.GetPaymentListByOrderId(orderId); err == orm.ErrNoRows {
		_, err = shopping.AddPaymentByPayMsg(orders.PartnerId,orders.StoreId,orders.MemberId,orderId,1,
                        "2","","CNY","","","","",orders.Payment,0,0,0)
		if err != nil {
		   return
		}
	    err = OrderPayAdditional(orderId)
		//
		SendPrintMsg(orders.PartnerId,orders.StoreId,orders.OrderId)
	}
	return
}
